//
//  SELoginViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SELoginViewController: SEBaseViewController {

    var loginSuccessHandler: ((Bool) -> Void)?

    var viewModel = SEAuthViewModel()

    @IBOutlet weak var loginTextView: SEAuthTextFildView! {
        didSet {
            loginTextView.viewType = .account
            loginTextView.textField.text = UserDefaults.userAccount
        }
    }
    @IBOutlet weak var passwordTextView: SEAuthTextFildView! {
        didSet {
            passwordTextView.viewType = .password
            passwordTextView.textField.text = UserDefaults.userPsd
        }
    }
    @IBOutlet weak var loginBtnView: SERegistrationConfirmBtnView! {
        didSet {
            loginBtnView.title = "登录"
            loginBtnView.btnBgColor = UIColor.init(hex: "03C691")
            loginBtnView.delegate = self
        }
    }


    @IBOutlet weak var rememberBtn: UIButton! {
        didSet {
            rememberBtn.isSelected = UserDefaults.remberAccountAndPsd
        }
    }

    @IBAction func forgetBtnAction(_ sender: UIButton) {
        pushToForget()
    }

    @IBAction func rememberBtnAction(_ sender: UIButton) {
        sender.isSelected.toggle()
        UserDefaults.remberAccountAndPsd = sender.isSelected
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        addEndEditingTap()
    }

    override func setupUI() {
        super.setupUI()
        viewModel.loginHandler = { status in
            switch status {
            case .success:
                self.perform(#selector(self.requestUserInfo), with: nil, afterDelay: 0.25)
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
                self.loginSuccessHandler?(false)
            }
        }

        viewModel.updateData = { status in
            switch status {
            case .success:
                AppDelegate.setupXylink()
                self.dismiss(animated: true) {
                    self.loginSuccessHandler?(true)
                }
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
                self.loginSuccessHandler?(false)
            }
        }
    }

    override func setupNavBar() {
        super.setupNavBar()
        navigationItem.leftBarButtonItem = UIBarButtonItem(
            image: Asset.navCloseIcon.image.withRenderingMode(.alwaysOriginal),
            style: .plain,
            target: self, action: #selector(leftItemAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(rightItemAction))
    }

    @objc private func leftItemAction() {
        dismiss(animated: true, completion: nil)
    }

    @objc private func rightItemAction() {
        pushToRegister()
    }

    private func pushToRegister() {
        let vc = SERegisterViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

    private func pushToForget() {
        let vc = SEForgetViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension SELoginViewController: SERegistrationConfirmBtnViewDelegate {
    func registrationConfirmBtnViewAction(view: SERegistrationConfirmBtnView) {
        self.view.endEditing(true)
        login()
    }
}

extension SELoginViewController {
    private func login() {
        viewModel.account = loginTextView.textField.text!
        viewModel.password = passwordTextView.textField.text!
        viewModel.requestLogin()
    }

    @objc private func requestUserInfo() {
        viewModel.requestUserInfo()
    }
}
